package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.goods.entity.category.BrandCategory;
import com.hoshiicloud.goods.param.category.BrandCategoryAddParam;
import com.hoshiicloud.goods.param.category.BrandCategoryQueryParam;
import com.hoshiicloud.goods.param.category.BrandCategoryUpdateParam;
import com.hoshiicloud.goods.service.BrandCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 品牌类目 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/brand-category")
@Api(value = "商品管理", tags = {"品牌类目"})
public class BrandCategoryController {
    @Autowired
    private BrandCategoryService brandCategoryService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询品牌类目
     */
    @ApiOperation(value = "分页查询品牌类目")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid BrandCategoryQueryParam param) {
        try {
            log.info("分页查询品牌类目");
            QueryWrapper<BrandCategory> queryWrapper = new QueryWrapper<BrandCategory>();
            queryWrapper.lambda().eq(param.getBrandId() != null, BrandCategory::getBrandId, param.getBrandId())
                    .eq(param.getCategoryId() != null, BrandCategory::getCategoryId, param.getCategoryId())
                    .eq(param.getType() != null, BrandCategory::getType, param.getType())
                    .eq(param.getDeleted() != null, BrandCategory::getDeleted, param.getDeleted());
            IPage<BrandCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "BrandCategoryQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            
            return Response.data(brandCategoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询品牌类目异常", e);
            return Response.failed("分页查询品牌类目异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询品牌类目
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询品牌类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(brandCategoryService.getById(id));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增品牌类目")
    public Response save(@RequestBody @Valid BrandCategoryAddParam param) {

        // Bean数据转换
        BrandCategory entity;
        try {
            entity = BeanUtils.covertType(param, BrandCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(brandCategoryService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改品牌类目
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改品牌类目")
    public Response update(@RequestBody @Valid BrandCategoryUpdateParam param) {

        // Bean数据转换
        BrandCategory entity;
        try {
            entity = BeanUtils.covertType(param, BrandCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(brandCategoryService.updateById(entity));
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 删除品牌类目
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除品牌类目")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(brandCategoryService.deleteById(id));
    }

    /**
     * @Author:campchen
     * @Description: StringUtils.join(ids, " ") 为缓存中添加唯一KEY  如不合适可选择更好方法
     * @Date:上午 10:18 2019/5/11
     * @Param: ids
     * @return: com.hoshiicloud.common.rpc.Response
     */
    @ApiOperation(value = "根据id集合查询品牌类目")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(brandCategoryService.getByIds(ids, StringUtils.join(ids, "")));
    }
}
